/**
 * 
 */
package org.jackysoft.entity;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.struts2.json.annotations.JSON;
import org.jackysoft.util.MySqlSchema;

/**
 * @author qujiakang
 * 
 */
@Entity
@Table(name = "department",schema=MySqlSchema.Identifyedbase)
public class Department extends LREntity implements Comparable<Department> {

    /**
	 * 
	 */
	private static final long serialVersionUID = 5852801939166078336L;
			
	public static final Department NONE = new Department();
	public static final String ROOTID="root";
	private  String name;
	
	private Department parent;
	
	private DepartmentType departmentType = DepartmentType.Manager;
		
	public Department() {
		super();
		this.setName("");		
	}
	
	public Department(String id){this.id = id;}
	
	public Department(String id,String name) {
		this.id = id;
		this.name = name;		
	}

	/**
	 * @return name
	 */
	
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            要设置的 name
	 */
	public void setName(String name) {
		this.name = name;
	}

	
	/*
	 * （非 Javadoc）
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof Department))return false;
		Department dept = (Department) obj;
		if(dept.getId()==null)return false;
	   return dept.getId().equals(this.id);
		
	}
	
	
	@Transient	
	@JSON(serialize = false)
	public Department getParent() {
		return parent;
	}

	public void setParent(Department parent) {
		this.parent = parent;
	}
	
	public void setDepartmentType(DepartmentType departmentType) {
		this.departmentType = departmentType;
	}
	public DepartmentType getDepartmentType() {
		return departmentType==null?DepartmentType.Manager:departmentType;
	}	
	
	@Override
	public int compareTo(Department o) {
		return this.sort-o.sort;
	}	

	
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
	}

	public boolean sameAs(Department d){
		if(d==null || d.id==null || this.id==null) return false;
		return this.id.equals(d.id);
	}
}
